﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class RightsManage
    {
        private static readonly RightsDal _myDAL = new RightsDal();

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="modelId"></param>
        public void Add(string modelId)
        {
            _myDAL.Add(modelId);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="modelId"></param>
        public void Delete(string modelId)
        {
            _myDAL.Delete(modelId);
        }

        public IList<Right> GetRights(bool isAdmin)
        {
            return _myDAL.GetRootRights(isAdmin);
        }

        public IList<Right> GetRights(int rootId, bool isAdmin)
        {
            IList<Right> list = _myDAL.GetRights(rootId, isAdmin);

            return (from x in list
                    select new Right
                    {
                        Id = x.Id,
                        Code = x.ModelId + x.Code,
                        Name = x.Name
                    }).ToList();
        }

        /// <summary>
        /// 角色的所有权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public IList<Right> GetRoleRights(int roleId)
        {
            return _myDAL.GetRoleRights(roleId);
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="userid"></param>
        public void Distribute(int userid)
        {
            var roleRightForUser = _myDAL.GetRights(userid);
            /*
             将来加入更多分析权限代码
             */
            _myDAL.Distribute(userid, roleRightForUser.Select(x => x.Id));
        }

        /// <summary>
        /// 分配功能权限到
        /// </summary>
        /// <param name="roleId"></param>
        public void DistributeForRole(int roleId)
        {
            /*
            将来加入更多分析权限代码
            */
            _myDAL.DistributeForRole(roleId);
        }

        /// <summary>
        /// 分配栏目权限到角色
        /// </summary>
        /// <param name="categoryIds"></param>
        /// <param name="roleid"></param>
        public void DistributeCategoryToRole(IEnumerable<int> categoryIds, int roleid)
        {
            new CategoryDal().SaveCategoryRole(categoryIds, roleid);
        }
    }
}